package com.leecode.exercise;

import java.util.HashSet;
import java.util.Set;

/**
 * @description:
 * @date: 2023/3/5 16:11
 * @author: yaoshubin
 * @version: 1.0
 */
public class LeeCode_3_无重复子串 {

    public static void main(String[] args) {
        // abcabcbb
        String s = "abcabecbb";
        int maxLen = lengthOfLongestSubstring(s);
        System.out.println(maxLen);

    }

    static int lengthOfLongestSubstring(String s) {
        int left = 0;
        int right = 0;
        int len = 0;
        int maxLen = 0;
        char[] chars = s.toCharArray();
        Set set = new HashSet();
        while (right < s.length()) {
            //不包含，说明无重复，right继续下一步
            if (!set.contains(chars[right])) {
                set.add(chars[right]);
                right++;
                len++;
                if (len > maxLen) {
                    maxLen = len;
                }
            } else {
                if (set.contains(chars[right])) {
                    set.remove(chars[left]);
                    left++;
                    len--;
                }
            }

        }
        return maxLen;
    }
}
